﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Utils;
using LevenBlog.Core.Service;

namespace LevenBlog.Core.Cache
{
    public static class CalendarCache
    {
        private static int maxsize = 50;
        private static Dictionary<Pair<int, int>, IList<int>> calendarCache = new Dictionary<Pair<int, int>, IList<int>>(maxsize);

        public static IList<int> Get(IDbProvider provider,int year, int month)
        {
            IList<int> cdata = null;
            Pair<int, int> key = new Pair<int, int>(year, month);
            if (!calendarCache.TryGetValue(key, out cdata))
            {
                cdata = Blog.GetWriteDays(provider, year, month);
                if (calendarCache.Count > maxsize)
                    calendarCache.Clear();
                calendarCache.Add(key, cdata);
            }
            return cdata;
        }

        public static void Clear()
        {
            calendarCache.Clear();
        }

        public static void Delete(int year, int month)
        {
            Pair<int, int> key = new Pair<int, int>(year, month);
            if (calendarCache.ContainsKey(key))
                calendarCache.Remove(key);
        }
    }
}
